import { addGlobalUncaughtErrorHandler, registerMicroApps, start } from 'qiankun'
import NProgress from 'nprogress'
import app from '@/micro/shared' // progress bar
NProgress.configure({ showSpinner: false })

const MID_HOST = process.env.VUE_APP_MID_HOST
const GY_HOST = process.env.VUE_APP_GY_HOST
const JN_HOST = process.env.VUE_APP_JN_HOST
export default function (changeLoading) {
  registerMicroApps([
    {
      name: 'ls-mid',
      entry: MID_HOST,
      container: '#app-qiankun',
      activeRule: ['/#/mid'],
      props: { app },
      loader: (loading) => {
        changeLoading(loading)
      }
    },
      {
          name: 'ls-jn',
          entry: JN_HOST,
          container: '#app-qiankun',
          activeRule: ['/#/jn'],
          props: { app },
          loader: (loading) => {
              changeLoading(loading)
          }
      },
    {
      name: 'ls-gy',
      entry: GY_HOST,
      container: '#app-qiankun',
      activeRule: ['/#/gy'],
      props: { app },
      loader: (loading) => {
        changeLoading(loading)
      }
    }
  ], {
    beforeLoad: () => {
      NProgress.start()
      return Promise.resolve()
    },
    afterMount: () => {
      NProgress.done()
      return Promise.resolve()
    }
  })
  addGlobalUncaughtErrorHandler((event) => {
    const { message } = event
    NProgress.done()
    changeLoading(false)
    if (message && message.includes('died in status LOADING_SOURCE_CODE')) {
      alert('微应用加载失败，请检查应用是否可运行')
    }
  })

  start({ prefetch: 'all', singular: false })
}
